﻿using System;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormHangHoaThem : DevComponents.DotNetBar.RibbonForm
    {
        public FormHangHoaThem()
        {
            InitializeComponent();
        }

        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();

        private void txtTenHangHoa_TextChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void cbbLoaiHangHoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void cbbDonViTinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void bttLuu_Click(object sender, EventArgs e)
        {
            Create();
        }

        private void bttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void txtDienGiai_TextChanged(object sender, EventArgs e)
        {
            KiemTra();
        }
        private void FormHangHoaThem_Load(object sender, EventArgs e)
        {
            GetListLoaiHang();
            GetListDonViTinh();
            KiemTra();
        }

        void Create()
        {
            try
            {
                var t = new HangHoa();
                t.DonViTinhId = Convert.ToInt32(cbbDonViTinh.SelectedValue);
                t.DienGiai = txtDienGiai.Text;
                t.LoaiHangId = Convert.ToInt32(cbbLoaiHangHoa.SelectedValue);
                t.SoLuong = 0;
                t.TenHangHoa = txtTenHangHoa.Text;
                db.HangHoas.Add(t);
                db.SaveChanges();
                MessageBox.Show("Thêm mới thành công!");
                Reset();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        void Reset()
        {
            txtDienGiai.Text = "";
            cbbDonViTinh.SelectedValue = 0;
            txtTenHangHoa.Text = "";
            cbbLoaiHangHoa.SelectedValue = 0;
            bttLuu.Enabled = false;
        }

        void GetListLoaiHang()
        {
            cbbLoaiHangHoa.DataSource = db.LoaiHangs.ToList();
            cbbLoaiHangHoa.DisplayMember = "TenLoaiHang";
            cbbLoaiHangHoa.ValueMember = "Id";
        }
        void GetListDonViTinh()
        {
            cbbDonViTinh.DataSource = db.DonViTinhs.ToList();
            cbbDonViTinh.DisplayMember = "TenDonViTinh";
            cbbDonViTinh.ValueMember = "Id";
        }
        void KiemTra()
        {
            if (txtDienGiai.Text != "" && Convert.ToInt32(cbbDonViTinh.SelectedValue) > 0 && txtTenHangHoa.Text != "" &&
                Convert.ToInt32(cbbLoaiHangHoa.SelectedValue) != 0)
            {
                bttLuu.Enabled = true;
            }
            else
            {
                bttLuu.Enabled = false;
            }
        }

        private void bt_ThemDonViTinh_Click(object sender, EventArgs e)
        {
            var a = new FormDonViTinhThem();
            a.ShowDialog();
        }

        private void bt_ThemLoaiHang_Click(object sender, EventArgs e)
        {
            var b = new FormLoaiHangThem();
            b.ShowDialog();
        }



        
    }
}
